﻿using System;
using System.Web.Mvc;

namespace Ams.Mvc.Bootstrap
{
    public enum Img
    {
        Normal,
        Rounded,
        Circle,
        Thumbnail
    }

    public static class BootstrapImageExtensions
    {
        #region Image()
        public static MvcHtmlString Image<TModel>(this BootstrapHelper<TModel> helper, string contentPath)
        {
            return Image(helper, contentPath, null, Img.Normal, null);
        }
        public static MvcHtmlString Image<TModel>(this BootstrapHelper<TModel> helper, string contentPath, Img imgType)
        {
            return Image(helper, contentPath, null, imgType, null);
        }
        public static MvcHtmlString Image<TModel>(this BootstrapHelper<TModel> helper, string contentPath, string altText, Img imgType)
        {
            return Image(helper, contentPath, altText, imgType, null);
        }
        public static MvcHtmlString Image<TModel>(this BootstrapHelper<TModel> helper, string contentPath, object htmlAttributes)
        {
            return Image(helper, contentPath, null, Img.Normal, htmlAttributes);
        }
        public static MvcHtmlString Image<TModel>(this BootstrapHelper<TModel> helper, string contentPath, string altText, object htmlAttributes)
        {
            return Image(helper, contentPath, altText, Img.Normal, htmlAttributes);
        }
        public static MvcHtmlString Image<TModel>(this BootstrapHelper<TModel> helper, string contentPath, string altText, Img imgType, object htmlAttributes)
        {
            var imgBuilder = new TagBuilder("img");

            var urlHelper = new UrlHelper(helper.ViewContext.HttpContext.Request.RequestContext);
            imgBuilder.MergeAttribute("src", urlHelper.Content(contentPath));

            if (!String.IsNullOrWhiteSpace(altText))
            {
                imgBuilder.MergeAttribute("alt", altText);
                imgBuilder.MergeAttribute("title", altText);
            }

            if (imgType != Img.Normal)
            {
                imgBuilder.AddCssClass("img-" + Enum.GetName(imgType.GetType(), imgType).ToLower());
            }

            imgBuilder.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
            return imgBuilder.ToMvcHtmlString(TagRenderMode.SelfClosing);
        } 
        #endregion
    }
}
